package application.common.cron4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;

import application.common.model.StoreUser;
import application.common.render.HttpClientResult;
import application.common.util.HttpUtil;
import application.common.util.ShiroUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StoreUserTask implements ITask{
	
	private final Log log = Log.getLog(getClass());
	private boolean isEnd=true;
	
	public void run() {
		if(isEnd) {
			isEnd=false;
			if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
				try {
					List<StoreUser> storeUsers=StoreUser.dao.find("select * from "+StoreUser.TABLE_NAME+" where src='1'");
					Map<String, String> params =new HashMap<String,String>();
					params.put("rows", (storeUsers!=null && storeUsers.size()>0) ? JsonKit.toJson(storeUsers):"");
					params.put("cabinetId", ShiroUtil.getAuth());
					HttpClientResult httpClientResult=HttpUtil.doPost(String.format("%s/api/cab/store/getUsers", ShiroUtil.getConfig().getSynURL()), params);
					if(httpClientResult==null) {
						isEnd=true;
						return;
					}
					if(httpClientResult.getCode()!=200) {
						isEnd=true;
						return;	
					}
					String content=httpClientResult.getContent();
					if(StrKit.isBlank(content)) {
						isEnd=true;
						return;	
					}
					JSONObject jsonObject=JSONObject.fromObject(content);
					if(jsonObject==null) {
						isEnd=true;
						return;	
					}
					log.info("同步用户数据="+content);
					if(jsonObject.has("code")) {
						int code=jsonObject.getInt("code");
						if(code!=200) {
							isEnd=true;
							return;	
						}
						if(jsonObject.has("data")) {
							String data=jsonObject.getString("data");
							if(StrKit.isBlank(data)) {
								isEnd=true;
								return;	
							}
							JSONArray array=JSONArray.fromObject(data);
							if(array==null || array.size()==0) {
								isEnd=true;
								return;
							}
							StringBuffer sb=new StringBuffer();
							for(Object object:array) {
								JSONObject row=JSONObject.fromObject(object);
								if(row!=null) {
									String id=row.getString("id");
									StoreUser storeUser=StoreUser.dao.findById(id);
									if(storeUser==null) {
										storeUser=new StoreUser();
									}
									if(!StrKit.equals(storeUser.getStoreId(), row.getString("storeId"))) {
										storeUser.setStoreId(row.getString("storeId"));
									}
									if(!StrKit.equals(storeUser.getUserId(), row.getString("userId"))) {
										storeUser.setUserId(row.getString("userId"));
									}
									if(StrKit.isBlank(storeUser.getId())) {
										storeUser.setId(id);
										storeUser.setSrc("0");
										storeUser.save();
									}
									else {
										storeUser.setSrc("0");
										storeUser.update();
									}
									sb.append("'").append(storeUser.getId()).append("'").append(",");
								}
							}
							if(!StrKit.isBlank(sb.toString())) {
								Db.delete("delete from "+StoreUser.TABLE_NAME+" where id not in ("+(sb.substring(0,sb.length()-1).toString())+") "); 
							}
							isEnd=true;
						}else {
							isEnd=true;
						}
					}else {
						isEnd=true;
					}
				}catch (Exception e) {
					log.info("日志同步异常！",e);
					isEnd=true;
				}
			}
			isEnd=true;
		}
	}

	public void stop() {
	}

}
